package com.sxyjhh.fg_data_push.utils.util.wsUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class Tools {

	public static final String DEFAULT_PATTERN = "yyyy-MM-dd";
	public static final String NUMBER_PATTERN = "0.00";
	
	/**
	 * 将DATE类型转成String类型,pattern默认为yyyy-MM-dd
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String getDateStr(Date date, String pattern) {
		if (date == null)
			date = new Date();
		if (pattern == null)
			pattern = DEFAULT_PATTERN;
		SimpleDateFormat formater = new SimpleDateFormat(pattern);
		return formater.format(date);
	}

	public static Date getDateByStr(String dateStr, String pattern)
			throws ParseException {
		if (dateStr == null || dateStr.equals(""))
			return new Date();
		if (pattern == null)
			pattern = DEFAULT_PATTERN;
		SimpleDateFormat formater = new SimpleDateFormat(pattern);
		return formater.parse(dateStr);
	}
	

	/**
	 * 组合交换返回结果xml字符串
	 * 
	 * @param list
	 * @param listParams
	 * @return
	 */

	public static String dxExchangeCreateXmlStr(List list, String[][] listParams,String query_Num) {

		String listInfo = "";
		String resultStr = "";
		long count = 0;
		long count1 = 0;

		// list_information
		// 业务数据
		if (list != null && list.size() > 0) {
			count1 = list.size();
			listInfo += createRowStr(list, listParams);
		}
		count = count1;
		resultStr += "<?xml version='1.0' encoding='UTF-8'?>\n";
		resultStr += "<exchange_data>\n";
		resultStr += Constants.INDENT + "<exchange_num>" + query_Num
				+ "</exchange_num>\n";
		resultStr += Constants.INDENT + "<exchange_count>" + count
				+ "</exchange_count>\n";
		resultStr += Constants.INDENT + "<exchange_rows>\n";
		resultStr += listInfo;
		resultStr += Constants.INDENT + "</exchange_rows>\n";
		resultStr += "</exchange_data>";
		return resultStr;
	}

	/**
	 * 组合Row标签Xml字符串
	 * 
	 * @return
	 */
	public static String createRowStr(List list, String[][] listParams) {
		String listInfo = "";

		if (list != null && listParams != null && list.size() > 0
				&& listParams.length > 0) {
			for (int i = 0; i < list.size(); i++) {
				listInfo += Constants.INDENT + Constants.INDENT
						+ "<exchange_row>\n";
				listInfo += createArgumentStr(list.get(i), listParams);
				listInfo += Constants.INDENT + Constants.INDENT
						+ "</exchange_row>\n";
			}
		}
		return listInfo;
	}

	/**
	 * 组合argument标签Xml字符串
	 * 
	 * @param obj
	 * @param objParams
	 * @return
	 */
	public static String createArgumentStr(Object obj, String[][] objParams) {

		if (obj == null || objParams.length == 0) {
			return null;
		}

		String itemStr = "";
		for (int i = 0; i < objParams.length; i++) {
			if (objParams[i][0] != null && !objParams[i][0].equals("")
					&& objParams[i][1] != null && !objParams[i][1].equals("")) {
				// <argument data_type='string' name='opType'
				// index='1'>IA</argument>
				itemStr += Constants.INDENT + Constants.INDENT
						+ Constants.INDENT + "<argument ";
				itemStr += " data_type=\"" + objParams[i][1] + "\" ";
				itemStr += " name=\"" + objParams[i][0] + "\" ";
				itemStr += " index=\"" + (i + 0) + "\" >";
				try {
					// 日期类型
					if (objParams[i][1].equals(Constants.DATA_TYPE_DATE)) {
						String date = BeanUtils.getProperty(obj,
								objParams[i][0]);
						Date dt = getDateByStr(date, null);
						String dateStr = getDateStr(dt, "yyyy-MM-dd");
						itemStr += dateStr;
						// 时间类型
					} else if (objParams[i][1]
							.equals(Constants.DATA_TYPE_DATETIME)) {
						String date = BeanUtils.getProperty(obj,
								objParams[i][0]);
						Date dt = getDateByStr(date, "yyyy-MM-dd HH:mm:ss");
						String dateStr = getDateStr(dt, "yyyy-MM-dd HH:mm:ss");
						itemStr += dateStr;
					} else {
						Object temp = BeanUtils.getProperty(obj, objParams[i][0]);
						itemStr += (temp == null) ? "" : filterSpecialCharOfXml(temp.toString());
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				itemStr += "</argument>\n";
			}
		}

		return itemStr;
	}
	
	/**
	 * 1、根据W3C标准，有一些字符不能出现在XML文件中：    0x00 - 0x08    0x0b - 0x0c    0x0e - 0x1f 我们直接替换为空，
	 * 2、'&'(实体引用的开始)和'<'(控制符的开始)和'>'作为XML的标准控制字符必须不能出现在正常内容中，我们直接过滤了
	 * @param txt
	 * @return
	 */
	public static String filterSpecialCharOfXml(String txt){
		String res = "";
	    	for(int i = 0; i < txt.length(); ++i){
	    		char ch = txt.charAt(i);
	    		if(	Character.isDefined(ch) &&
	    			ch!= '&' && ch != '<' && ch != '>' && ch != '/' && 
	    			!Character.isHighSurrogate(ch) &&
	    			!Character.isISOControl(ch) &&
	    			!Character.isLowSurrogate(ch)
	    		   ){
	    			res = res + ch;
	    		}
	    	}
	    	return res.replaceAll("[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]", "");
	}
	
	
	
}
